/** 创建组件对象 * */
var benchmarkForm = new Form();
var benchmarkTable = new Table();
var benchmarkDialog = new Dialog();
var benchmarkAjax = new Ajax();
var benchmarkList = [];
//定义一个全局变量dataIdList
var dataList = [];
/**
 * 页面加载完执行
 * 
 * @param
 * @return
 */
$(function() {
	initScript();
	initButtons();
	resetTableHeight();
	pageQuery();
	benchmarkTable.reloadData();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/benchmark/locale/Benchmark_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/benchmark/properties/BenchmarkProperties_View.js"); // 2.加载配置js(非必配,配置也可写在本js中)
}

/** 初始化按钮 * */
function initButtons() {
	benchmarkTable.initButtons({
		divId : "benchmarkButtonDiv", // 1.归属div*
		buttons : BenchmarkProperties_View.getViewButtons()
	});
	disableButton();
}

function disableButton() {
	setElementsEnabled("yddemo:benchmark:save", "button", false);
	setElementsEnabled("yddemo:benchmark:reset", "button", false);
}

function enableButton() {
	setElementsEnabled("yddemo:benchmark:save", "button", true);
	$("[action='yddemo:benchmark:save']").unbind("click").bind("click",
			function() {
				saveBenchmark();
			});
	setElementsEnabled("yddemo:benchmark:reset", "button", true);
	$("[action='yddemo:benchmark:reset']").unbind("click").bind("click",
			function() {
				resetBenchmark();
			});
}

/** 分页查询 * */
function pageQuery() {
	if (sessionStorage.orgCode == "*") {
		disableButton();
	}else{
		enableButton();
	}
	benchmarkList = benchmarkAjax.sendAjax({
		url : requestPath + "/yddemo/benchmarks", // 请求url*
		async : false, // 是否异步*
		params : {
			nodeId : sessionStorage.nodeId
		},
		type : "GET" // 请求类型*
	});
	benchmarkTable.initTable({
		divId : "benchmarkTableDiv", // 1.归属div*
		dataAjax : { // 2.发送ajax请求*
			url : requestPath + "/yddemo/benchmarks", // 请求url*
			async : false, // 是否异步*
			params : {
				nodeId : sessionStorage.nodeId
			},
			type : "GET" // 请求类型*
		// 分页对象(需要分页时配置)
		},
		columns : BenchmarkProperties_View.getViewColumns()
	// 3.表格列配置
	});
	if (benchmarkList.length == 0) {
		disableButton();
	}
}

/** 保存 * */
function saveBenchmark() {
	var resultJson = benchmarkAjax.sendAjax({
		url : requestPath + "/yddemo/benchmarks", // 1.请求url*
		params : { // 2.请求url参数(非必配)
			_method : "PUT"
		},
		async : false, // 3.是否异步*
		type : "POST", // 4.请求类型*
		data : dataList
	// 5.请求参数
	});
	if (resultJson.success) {
		benchmarkDialog.tipDialog(benchmarkLocale.text.saveSuccess,
				function() {
					benchmarkTable.reloadData();
					benchmarkDialog.closeDialog(); // 修改成功后,关闭父对象弹出框(关闭ManualPoint_Edit.jsp页面)
					benchmarkList = [];
				});
	} else {
		benchmarkDialog.alertDialog(resultJson.message); // 修改失败后,父对象弹出警告框
	}
	// }
}

/** 重置 * */
function resetBenchmark() {
	benchmarkTable.reloadData();
	dataList = [];
}

/**
 * 当修改文本框内的数据时执行
 * 
 * @param e
 * @returns
 */
function getTdData(e) {
	stopProp();
	setTimeout(function() {
		if (/^([0-9]*)$/.test(e.value) != true) {
			e.value = e.value.replace(/[^\d]/g, '');
			return false;
		}
		var dataObj = null;
		var rowIndex = $(e.parentNode).index();
		rowDataObejct = benchmarkTable.dataList[rowIndex];
		if (rowDataObejct.designValue == e.value) {
			e.style.backgroundColor = "white";
			if (dataList.length != 0) {
				for (var i = 0; i < benchmarkList.length; i++) {
					if (dataList[i].indicatorId == rowDataObejct.indicatorId) {
						dataList.splice(i, 1);
					}
				}
			}
			return false;
		} else {
			e.style.backgroundColor = "yellow";
			if (dataList.length == 0) {
				dataObj = {};
				dataObj.indicatorId = rowDataObejct.indicatorId;
				dataObj.designValue = e.value;
				dataObj.detailId = rowDataObejct.detailId;
			} else {
				for (var i = 0; i < dataList.length; i++) {
					if (dataList[i].indicatorId == rowDataObejct.indicatorId) {
						dataList[i].designValue = e.value;
						dataObj = null;
						break;
					} else {
						dataObj = {};
						dataObj.indicatorId = rowDataObejct.indicatorId;
						dataObj.designValue = e.value;
						dataObj.detailId = rowDataObejct.detailId;
					}
				}
			}
			if (dataObj != null) {
				dataList.push(dataObj);
			}
		}
	}, 1);
}

/**
 * 重设表格高度
 * 
 * @param
 * @return
 */
function resetTableHeight() {
	var leftHeight = $(".dataListDiv").height()
			- getTotalHeight([ "benchmarkButtonDiv",
					"benchmarkPageDiv" ]);
	$("#benchmarkTableDiv").height(leftHeight);
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight();
});